/*
 * @Descripttion:
 * @version:
 * @Author: ZmSama
 * @Date: 2022-01-11 11:36:19
 */
// 获取当月总天数
function getDayNum(val: any) {
  let date = new Date(val);
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let d = new Date(year, month, 0);
  return d.getDate();
}

// 格式化日期yyyy-mm-dd
function formatDate(date: any) {
  date = new Date(date);
  let myyear = date.getFullYear();
  let mymonth = date.getMonth() + 1;
  let myweekday = date.getDate();
  let monthStr: string = mymonth < 10 ? "0" + mymonth : mymonth;
  let dayStr: string = myweekday < 10 ? "0" + myweekday : myweekday;
  return {
    year: myyear,
    month: monthStr,
    day: dayStr,
  };
}
// 判断某一天是星期几
function getWeek(date: any) {
  let dt2 = new Date(date);
  var weekDay = [
    "星期天",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六",
  ];
  if (weekDay.indexOf(weekDay[dt2.getDay()]) > -1) {
    return weekDay[dt2.getDay()];
  }
  return "";
}
function dateGenerator(val: any) {
  let now = new Date(val);
  let dayLength = 0;
  let currentMonth = [];
  dayLength = getDayNum(now);
  for (let i = 1; i <= dayLength; i++) {
    let nowDate = now.setDate(i);
    const { year, month, day } = formatDate(nowDate);
    let everyDay = year + "-" + month + "-" + day;
    let week = getWeek(everyDay);
    currentMonth.push({ date: month + "-" + day, week: week });
  }
  return currentMonth;
}

export default dateGenerator;
